package com.gxp.system.service.impl;

import com.gxp.common.core.text.Convert;
import com.gxp.common.utils.DateUtils;
import com.gxp.common.utils.ShiroUtils;
import com.gxp.common.utils.StringUtils;
import com.gxp.system.domain.ZkUserSvtype;
import com.gxp.system.domain.ZknjMethod;
import com.gxp.system.domain.ZknjService;
import com.gxp.system.domain.dto.ServicesDto;
import com.gxp.system.mapper.ZkUserSvtypeMapper;
import com.gxp.system.mapper.ZknjMethodMapper;
import com.gxp.system.mapper.ZknjReqSamInfMapper;
import com.gxp.system.mapper.ZknjServiceMapper;
import com.gxp.system.service.IZknjServiceService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.stream.Collectors;

/**
 * 服务2Service业务层处理
 * 
 * @author zh
 * @date 2024-07-18
 */
@Service
public class ZknjServiceServiceImpl implements IZknjServiceService 
{
    @Autowired
    private ZknjServiceMapper zknjServiceMapper;
    @Autowired
    private ZknjReqSamInfMapper reqSamMapper;
    @Autowired
    private ZknjMethodMapper methodMapper;
    @Autowired
    private ZkUserSvtypeMapper zkUserSvtypeMapper;

    /**
     * 查询服务2
     * 
     * @param serId 服务2主键
     * @return 服务2
     */
    @Override
    public ZknjService selectZknjServiceBySerId(Long serId)
    {
        return zknjServiceMapper.selectZknjServiceBySerId(serId);
    }

    /**
     * 查询服务2列表
     * 
     * @param zknjService 服务2
     * @return 服务2
     */
    @Override
    public List<ZknjService> selectZknjServiceList(ZknjService zknjService)
    {
        return zknjServiceMapper.selectZknjServiceList(zknjService);
    }

    /**
     * 新增服务2
     * 
     * @param zknjService 服务2
     * @return 结果
     */
    @Override
    public int insertZknjService(ZknjService zknjService)
    {
        zknjService.setCreateTime(DateUtils.getNowDate());
        zknjService.setSerStatus(2l);
        zknjService.setApproveType("Add Service");
        zknjService.setIsLock(0l);
        return zknjServiceMapper.insertZknjService(zknjService);
    }

    /**
     * 修改服务2
     * 
     * @param zknjService 服务2
     * @return 结果
     */
    @Override
    public int updateZknjService(ZknjService zknjService)
    {
        zknjService.setUpdateTime(DateUtils.getNowDate());
        zknjService.setIsLock(zknjService.getSerStatus());
        zknjService.setSerStatus(2l);
        zknjService.setApproveType("Update Service");
        return zknjServiceMapper.updateZknjService(zknjService);
    }

    /**
     * 批量删除服务2
     * 
     * @param serIds 需要删除的服务2主键
     * @return 结果
     */
    @Override
    public int deleteZknjServiceBySerIds(String serIds)
    {
        String[] strArray = Convert.toStrArray(serIds);
        for (String s : strArray) {
            Long serId = Long.valueOf(s);
            ZknjService zknjService = new ZknjService();
            zknjService.setSerId(serId);
            zknjService.setSerStatus(2l);
            zknjService.setApproveType("Delete Service");
            zknjServiceMapper.updateZknjService(zknjService);
        }
        return 1;
    }

    /**
     * 删除服务2信息
     * 
     * @param serId 服务2主键
     * @return 结果
     */
    @Override
    public int deleteZknjServiceBySerId(Long serId)
    {
        return zknjServiceMapper.deleteZknjServiceBySerId(serId);
    }

    /**
     * 对象条件查询（填充Instrument、serviceType）
     * @param zknjService
     * @return
     */
    @Override
    public List<ZknjService> selectZknjServices(ZknjService zknjService) {
        return zknjServiceMapper.selectZknjServices(zknjService);
    }

    /**
     * 服务状态修改
     * @param zknjService
     * @return
     */
    @Override
    public int changeStatus(ZknjService zknjService) {
        return zknjServiceMapper.updateZknjService(zknjService);
    }

    /**
     * 根据仪器ID获取服务
     * @param instIds
     * @return
     */
    @Override
    public List<ZknjService> selectByInstIds(String[] instIds) {
        return zknjServiceMapper.selectByInstIds(instIds);
    }

    @Override
    public ServicesDto selectZknjServiceAndMethodByModIds(String modIds) {
        String[] ids = modIds.split(",");
        List<ZknjMethod> methods = methodMapper.selectZknjMethodByModIds(ids);
        List<Long> collect = methods.stream().map(ZknjMethod::getSerId).collect(Collectors.toList());
        String join = StringUtils.join(collect, ",");
        String[] serIds = join.split(",");
        List<ZknjService> services = zknjServiceMapper.selectZknjServiceBySerIds(serIds);
        ServicesDto servicesDto = new ServicesDto();
        servicesDto.setMethods(methods);
        servicesDto.setServices(services);
        return servicesDto;
    }

    /**
     * 根据reqId查询Services
     */
    @Override
    public List<ZknjService> selectZknjServicesByReqId(Long reqId, boolean isGiveUserPermissionFlag) {
        List<ZknjService> services = zknjServiceMapper.selectZknjServicesByReqId(reqId);
        if(isGiveUserPermissionFlag){
            //查询用户服务类型权限
            if (ShiroUtils.getLoginName().equals("admin")) {
                //admin 全权限
                services.forEach(service -> service.setUserPermissionFlag(true));
            }else {
                Long userId = ShiroUtils.getUserId();
                List<Long> userSvtypeIds = zkUserSvtypeMapper.selectZkUserSvtypeByUserId(userId).stream().map(ZkUserSvtype::getSvtypeId).collect(Collectors.toList());
                if(userSvtypeIds.isEmpty()){
                    //未设置权限的，全权限
                    services.forEach(service -> service.setUserPermissionFlag(true));
                }
                services.forEach(service -> {
                    if(userSvtypeIds.contains(service.getSerTypeId())){
                        service.setUserPermissionFlag(true);
                    }else {
                        service.setUserPermissionFlag(false);
                    }
                });
            }
        }
        return services;
    }

    /**
     * 服务审核
     * @param service
     * @return
     */
    @Override
    public int approve(ZknjService service) {
        if(service.getSerStatus() == 3){
            ZknjService serviceOld = zknjServiceMapper.selectZknjServiceBySerId(service.getSerId());
            service.setSerStatus(serviceOld.getIsLock());
            List<ZknjMethod> methods = methodMapper.selectZknjMethodBySerId(service.getSerId());
            if(methods == null || methods.isEmpty()){
                service.setSerStatus(6l);
            }

            if(serviceOld.getApproveType().equals("Delete Service")){
                methodMapper.deleteZknjMethodBySerId(service.getSerId());
                return zknjServiceMapper.deleteZknjServiceBySerId(service.getSerId());
            }
        }
        return zknjServiceMapper.updateZknjService(service);
    }
}
